package cn.le.thread;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * synchronized 只是加锁当前对象,如果当前对象调用了其中一个synchronized方法,
 * 那么此对象其他的synchronized方法只能等待其调用完成
 *
 */
public class SynTest {

    private int  size;

    private Lock lock = new ReentrantLock();

    public synchronized int getSize()
    {
        lock.lock();
        size++;
        lock.unlock();
        return size;
    }
}


class Thread1 extends Thread{

    public void run()
    {
        SynTest synTest = new SynTest();
        System.out.println("Thread1:"+synTest.getSize());
    }
}
class Thread2 extends Thread{

    public void run()
    {
        SynTest synTest = new SynTest();
        System.out.println("Thread2:"+synTest.getSize());
    }
}




class run{

    public static void main(String[] args) throws Exception{

        Thread1 thread1 = new Thread1();
        thread1.run();
        Thread2 thread2 = new Thread2();
        thread2.run();

    }

}





